package 极客时间.concurrence.并发编程理论基础.面向对象写多线程;

import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;

/**
 * @Author idea
 * @Date created in 8:12 上午 2020/7/22
 */
public class Counter {

    private int total = 0;

    public void incr(CountDownLatch countDownLatch) throws InterruptedException {
        countDownLatch.await();
        total = total + 1;
    }

    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 1000; i++) {
            Thread t = new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    counter.incr(countDownLatch);
                    if (counter.total == 1000) {
                        System.out.println("数据正常");
                    } else {
                        System.out.println("数据异常");
                    }
                }
            });
            t.start();
        }
        System.out.println("所有线程准备就绪");
        Thread.sleep(1000);
        System.out.println("开始===");
        countDownLatch.countDown();
        while (true) {

        }
    }
}
